package ir_v3

import (
	"github.com/astaxie/beego/orm"
	"log"
)

type Camera struct {
	Id         int    `orm:"Column(Id)"`
	DeviceId   string `orm:"Column(DeviceId)"`
	DeviceName string `orm:"Column(DeviceName)"`
	MacAddr    string `orm:"Column(MacAddress)"`
	IPaddr     string `orm:"Column(IpAddress)"`
	MallId     string `orm:"Column(MallId)"`
	Enabled    int    `orm:"Column(Enabled)"`
}

func (u *Camera) TableName() string {
	return CameraTBName()
}

func InsertCamera(c Camera) (int64, error) {
	o := orm.NewOrm()
	return o.InsertOrUpdate(&c)
}

func FindAllCamera() (*[]Camera, error) {
	o := orm.NewOrm()
	o.Using("default")
	qs := o.QueryTable(CameraTBName())
	cameras := new([]Camera)
	_, err := qs.All(cameras)
	return cameras, err
}

func UpdateCmareMallId(deviceId, mallId string) {
	o := orm.NewOrm()
	res, err := o.Raw("UPDATE d_cameras SET MallId = ? WHERE DeviceId = ?", mallId, deviceId).Exec()
	if err == nil {
		res.RowsAffected()
	} else {
		log.Println(err)
	}
}
